"use strict";

const crypto = require("crypto");

/**
 * @param {Egg.Application} app - egg app
 */
module.exports = app => {

    const { Sequelize, Model } = app.Sequelize;
    const sequelize = app.model;

    class Users extends Model {
        /**
         * 检查邮箱是否已被使用
         * @param {string} email 邮箱
         * @returns 查询结果
         */
        async checkEmail(email) {
            const result = await sequelize.Users.findOne({
                where: {
                    email,
                },
                attributes: [ "id" ],
                raw: true,
            });
            return result != null;
        }

        /**
         * 加密密码
         * @param {string} password 密码
         * @returns 加密后的密码
         */
        hasher(password) {
            return crypto.createHash("md5").update(String(password)).digest("hex");
        }

        /**
         * 判断密码是否正确
         * @param {string} password 密码
         * @returns 判断结果
         */
        async checkPassword(password) {
            return this.password === this.hasher(password);
        }
    }

    Users.init({
        id: {
            type: Sequelize.INTEGER(11).UNSIGNED,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            comment: "用户 ID",
        },
        email: {
            type: Sequelize.STRING(255),
            allowNull: false,
            comment: "邮箱",
        },
        password: {
            type: Sequelize.STRING(100),
            allowNull: false,
            comment: "密码",
            set(value) {
                const password = this.hasher(value || Date.now());
                this.setDataValue("password", password);
            },
        },
        nickname: {
            type: Sequelize.STRING(100),
            allowNull: true,
            comment: "昵称",
            async set(value) {
                let nickname = String(value).replace(/\s/g, "");
                if (!nickname) {
                    nickname = `萌新_${Date.now()}`;
                }
                console.log(nickname);
                this.setDataValue("nickname", nickname);
            },
        },
        introduce: {
            type: Sequelize.STRING(255),
            allowNull: false,
            defaultValue: "",
            comment: "个人介绍",
        },
        avatar: {
            type: Sequelize.STRING(255),
            allowNull: true,
            comment: "头像",
        },
        pendant: {
            type: Sequelize.STRING(255),
            allowNull: true,
            comment: "头像框",
        },
        gender: {
            type: Sequelize.INTEGER(1).UNSIGNED,
            allowNull: false,
            defaultValue: 0,
            comment: "性别 0:保密 1:男 2:女",
        },
        status: {
            type: Sequelize.INTEGER(1).UNSIGNED,
            allowNull: false,
            defaultValue: 0,
            comment: "用户状态：0 正常 1 禁用 2 审核中 3 审核拒绝 4 审核忽略",
        },
        fans_count: {
            type: Sequelize.INTEGER(10).UNSIGNED,
            allowNull: false,
            defaultValue: 0,
            comment: "粉丝数",
        },
        follow_count: {
            type: Sequelize.INTEGER(10).UNSIGNED,
            allowNull: false,
            defaultValue: 0,
            comment: "关注数",
        },
        liked_count: {
            type: Sequelize.INTEGER(10).UNSIGNED,
            allowNull: false,
            defaultValue: 0,
            comment: "点赞数",
        },
        login_at: {
            type: Sequelize.DATE,
            allowNull: true,
            comment: "最后登录时间",
        },
        created_at: {
            type: Sequelize.DATE,
            allowNull: true,
            comment: "创建时间",
        },
    }, {
        comment: "用户表",
        tableName: "users",
        timestamps: true,
        updatedAt: false,
        indexes: [
            { fields: [ "nickname" ], unique: true },
            { fields: [ "email" ], unique: true },
        ],
        // 这是其他模型参数
        sequelize, // 我们需要传递连接实例
        modelName: "Users", // 我们需要选择模型名称
    });

    // 关联设置
    Users.associate = () => {

    };

    return Users;
};
